import {
    getShopsById,
    queryIsLottery,
    addLottery,
    queryLotteryWinner,
    addEnroll,
    getEnroll
} from "@/api/activity";
import {
    getSessionStorageObj,
    setSessionStorageObj,
    getStorageObj,
    setStorageObj,
    isSameDay
} from "../js/utils";
import { aid } from "@/config/shareConfig";

function checkIsClickShare() {
    let res = getStorageObj("isClickShare");
    if (res) {
        let curDate = new Date();
        let storageDate = res.createTime;
        return isSameDay(curDate, storageDate);
    } else {
        return false;
    }
}

export default {
    state: {
        shopsMsg: {},
        drawNum: 0, //剩余抽奖次数
        drawPrizeMsg: {}, //当前中奖
        drawRecord: [], //中奖记录
        winnerRecord: [], //总中奖记录
        isClickShare: checkIsClickShare(),
        enrollMsg: null
    },
    getters: {},
    mutations: {
        setShopsMsg(state, payload) {
            state.shopsMsg = payload;
        },
        setDrawNum(state, payload) {
            state.drawNum = payload;
        },
        setDrawPrizeMsg(state, payload) {
            state.drawPrizeMsg = payload;
        },
        setDrawRecord(state, payload) {
            state.drawRecord = payload;
        },
        setIsClickShareTrue(state, payload) {
            state.isClickShare = true;
            setStorageObj("isClickShare", {
                createTime: new Date()
            });
        },
        setWinnerRecord(state, payload) {
            state.winnerRecord = payload;
        },
        setEnrollMsg(state, payload) {
            state.enrollMsg = payload;
        }
    },
    actions: {
        getShopsById({}, payload) {
            return new Promise((resolve, reject) => {
                getShopsById(payload)
                    .then(res => {
                        resolve(res);
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        queryIsLottery({ commit }, payload) {
            return new Promise((resolve, reject) => {
                queryIsLottery(payload)
                    .then(res => {
                        if (res && res.code == 0) {
                            if (
                                Array.isArray(res.data) &&
                                res.data.length > 0
                            ) {
                                let arr = [];
                                res.data.forEach(e => {
                                    try {
                                        e.prize = JSON.parse(e.prize);
                                    } catch (error) {
                                        console.log(error);
                                    }
                                    if (e.id == aid) {
                                        arr.push(e);
                                    }
                                });
                                commit("setDrawRecord", arr);
                            } else {
                                commit("setDrawRecord", []);
                            }
                        }
                        resolve(res);
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        addLottery({}, payload) {
            return new Promise((resolve, reject) => {
                addLottery(payload)
                    .then(res => {
                        resolve(res);
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        queryLotteryWinner({}, payload) {
            return new Promise((resolve, reject) => {
                queryLotteryWinner(payload)
                    .then(res => {
                        resolve(res);
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        addEnroll({}, payload) {
            return new Promise((resolve, reject) => {
                addEnroll(payload)
                    .then(res => {
                        resolve(res);
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        getEnroll({ commit }, payload) {
            return new Promise((resolve, reject) => {
                getEnroll(payload)
                    .then(res => {
                        if (res && res.code == 0) {
                            if (Array.isArray(res.data)) {
                                if (res.data.length == 0) {
                                    commit("setEnrollMsg", null);
                                } else {
                                    commit("setEnrollMsg", res.data[0]);
                                }
                            }
                        }
                        resolve(res);
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        }
    },
    namespaced: true
};
